/*
 * Copyright (C) 2013 Surviving with Android (http://www.survivingwithandroid.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tecnoagrosoft.zafra.control.activity.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tecnoagrosoft.zafra.control.activity.R;
import com.tecnoagrosoft.zafra.control.model.DayForecast;

/**
 * @author 
 *
 */
public class DayForecastFragment extends Fragment {
	
	private DayForecast dayForecast;
	private ImageView iconWeather;
	
	private int wId;
	private String iconoId;
	
	public DayForecastFragment() {}
	
	public void setForecast(DayForecast dayForecast) {
		
		this.dayForecast = dayForecast;
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.dayforecast_fragment, container, false);
		
		TextView tempView = (TextView) rootView.findViewById(R.id.tempForecast);
		TextView descView = (TextView) rootView.findViewById(R.id.skydescForecast);
		tempView.setText( (int) (dayForecast.forecastTemp.min) + "-" + (int) (dayForecast.forecastTemp.max));
		descView.setText(dayForecast.weather.currentCondition.getDescr());
		iconWeather = (ImageView) rootView.findViewById(R.id.forCondIcon);
		
		// Now we retrieve the weather icon
		String icon = dayForecast.weather.currentCondition.getIcon();
		int weatherId = dayForecast.weather.currentCondition.getWeatherId();
		
//		Log.e("icon", icon);
//		Log.e("weatherId", getString(weatherId));
		
		loadIcon(weatherId, icon);
		
		return rootView;
	}

	
	
	private void loadIcon(int weatherId, String icon) {

		iconWeather.setImageResource(ClimaFragment.getImage(wId, iconoId));
		
	}


//	private class JSONIconWeatherTask extends AsyncTask<String, Void, byte[]> {
//		
//		@Override
//		protected byte[] doInBackground(String... params) {
//			
//			byte[] data = null;
//			
//			try {
//				
//				// Let's retrieve the icon
//				data = ( (new WeatherHttpClient()).getImage(params[0]));
//				
//			} catch (Exception e) {				
//				e.printStackTrace();
//			}
//			
//			return data;
//	}
		
		
		
		
//	@Override
//		protected void onPostExecute(byte[] data) {			
//			super.onPostExecute(data);
//			
//			if (data != null) {
//				Bitmap img = BitmapFactory.decodeByteArray(data, 0, data.length); 
//				iconWeather.setImageBitmap(img);
//			}
//		}
//
//
//
//  }
}
